<?php


namespace App\store\services;


use App\store\models\OrderModel;

class OrderServices
{

    /**
     * 获取 - 订单列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getOrderList($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }
        if (!isset($data['page']) && !isset($data['limit'])) {
            errorJson('没有携带分页查询参数！', []);
        }
        $page = !isset($data['page']) ? 0 : $data['page'] - 1;unset($data['page']);
        $limit = !isset($data['limit']) ? 0 : $data['limit'];unset($data['limit']);

        $where_data = [];
        // 订单状态：必填, 0 全部, 1 待付款, 2 待发货, 3 待收货, 4 已完成, 5 已关闭
        if (isset($data['status']) && !empty($data['status'])) {
            $where_data[] = ['status','=',$data['status']];
        }
        // 订单类型：可选参数, 1 普通订单, 2 积分抽奖, 3 优惠券订单
        if (isset($data['order_type']) && !empty($data['order_type'])) {
            $where_data[] = ['order_type','=',$data['order_type']];
        }
        // 查询时间类型：可选参数, 1 下单时间, 2 支付时间, 3 发货时间, 4 完成时间
        if (isset($data['time_type']) && !empty($data['time_type'])) {
            switch ($data['time_type']) {
                case 1: $select_time = 'create_time';break; // 下单时间
                case 2: $select_time = 'pay_time';break; // 支付时间
                case 3: $select_time = 'send_time';break; // 发货时间
                case 4: $select_time = 'finish_time';break; // 完成时间
                default: $select_time = 'create_time';
            }
            if (isset($data['start_time']) && !empty($data['start_time'])) {
                $start_time = strtotime($data['start_time']);
                $where_data[] = [$select_time, '>=', $start_time];
            }
            if (isset($data['end_time']) && !empty($data['end_time'])) {
                $end_time = strtotime($data['end_time']);
                $where_data[] = [$select_time, '<=', $end_time];
            }
        } else {
            errorJson('没有选择对应的查询时间类型！', []);
        }
        // 商品类型: 可选参数, 1 实体商品, 2 虚拟商品, 3 电子卡密
        if (isset($data['goods_type']) && !empty($data['goods_type'])) {
            $where_data[] = ['goods_type', '=', $data['goods_type']];
        }
        // 配送类型： 可选参数, 1 自提, 2 快递
        if (isset($data['dispatch_type_id']) && !empty($data['dispatch_type_id'])) {
            $where_data[] = ['dispatch_type_id', '=', $data['dispatch_type_id']];
        }
        // 售后类型：可选参数, 1 未售后, 2 售后中, 3 售后完成
        if (isset($data['refund_id']) && !empty($data['refund_id'])) {
            $where_data[] = ['refund_id', '=', $data['refund_id']];
        }
        // 支付方式：可选参数, 1 余额支付, 2 佣金支付, 3 微信支付, 4 后台确认, 5 积分+余额, 6 积分+零钱, 7 积分+微信
        if (isset($data['pay_type']) && !empty($data['pay_type'])) {
            $where_data[] = ['pay_type', '=', $data['pay_type']];
        }
        // 订单来源：可选参数, 1 微信小程序, 2 h5
        if (isset($data['order_source']) && !empty($data['order_source'])) {
            $where_data[] = ['order_source', '=', $data['order_source']];
        }

        $where_in_data = [];
        if (isset($data['goods_name']) && !empty($data['goods_name'])) {
            $select_data[] = ['title', 'like', "%{$data['goods_name']}%"];
            $ids = OrderModel::sonOrderIdListed($select_data);
            $where_in_data = array_column($ids, 'order_id');
        }

        $data = OrderModel::listed($page, $limit, $where_data, $where_in_data, $data);
        if (!$data) {
            errorJson('查询数据失败！', []);
        }

        foreach ($data as $k => $v) {
            $select_arr[] = ['order_id', '=', "{$v['id']}"];
            $data[$k]['goods_list'] = OrderModel::sonListed($select_arr);
        }

        return $data;
    }

    /**
     * 商户详情
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getOrderDetail($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }
        $where_data = [];
        if (isset($data['store_id']) && !empty($data['store_id'])) {
            $where_data[] = ['store_id', '=', $data['store_id']];
        }
        if (isset($data['order_id']) && !empty($data['order_id'])) {
            $where_data[] = ['id', '=', $data['order_id']];
        }
        $data = OrderModel::detailsed($where_data);
        if (!$data) {
            errorJson('查询数据失败！', []);
        }

        foreach ($data as $k => $v) {
            $select_arr[] = ['order_id', '=', "{$v['id']}"];
            $data[$k]['goods_list'] = OrderModel::sonListed($select_arr);
        }

        return $data;
    }

}
